package com.xhwl.centiotdata.handler.info.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.info.InfoDeviceFactory;
import com.xhwl.centiotdata.handler.info.InfoDeviceHandler;
import com.xhwl.centiotdata.pojo.dto.haikanginfo.DeviceHaiKangInfoDTO;
import com.xhwl.centiotdata.pojo.dto.haikanginfo.InfoIntegrationPropertiesDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.suppliersystem.haikang.DeviceHaiKangInfoHandler;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author： wuyuhao
 * @Description：
 * @Date 2022/7/29
 */
@Service
public class DeviceIntegrationHaiKangInfoHandler extends InfoDeviceHandler {

    @Autowired
    private DeviceHaiKangInfoHandler deviceHaiKangInfoHandler;

    @Autowired
    private IDeviceCentService deviceCentService;

    @Override
    public void afterPropertiesSet() throws Exception {
        InfoDeviceFactory.register(ProductEnum.HAIKANG_INFO.getId(), this);
    }

    public void pullInfoDevice(InfoIntegrationPropertiesDTO propertiesDTO) {
        switch (propertiesDTO.getDeviceType()) {
            case 47:
                this.pullDevice(propertiesDTO);
                break;
        }
    }

    private void pullDevice(InfoIntegrationPropertiesDTO propertiesDTO) {
        // 拉取信息发布系统的设备数据，里面包含了在离线状态
        List<DeviceHaiKangInfoDTO> deviceHaiKangInfoDTOList = deviceHaiKangInfoHandler.pullDevice(propertiesDTO);
        if (CollectionUtils.isEmpty(deviceHaiKangInfoDTOList)) {
            return;
        }
        // 转换数据
        List<DeviceDTO> deviceDTOList = this.handleDevice(propertiesDTO, deviceHaiKangInfoDTOList);
        // 存储业务中台
        deviceCentService.asyncUpdateDevice(deviceDTOList);
    }

    private List<DeviceDTO> handleDevice(InfoIntegrationPropertiesDTO propertiesDTO, List<DeviceHaiKangInfoDTO> deviceHaiKangInfoDTOList) {
        return deviceHaiKangInfoDTOList.stream().map(deviceHaiKangInfoDTO -> {
            DeviceDTO deviceDTO = new DeviceDTO();
            deviceDTO.setProjectId(propertiesDTO.getProjectId());
            deviceDTO.setProductId(propertiesDTO.getProductId());
            deviceDTO.setCategoryId(propertiesDTO.getCategoryEnum().getId());
            deviceDTO.setDeviceTypeId(propertiesDTO.getDeviceType());
            deviceDTO.setName(deviceHaiKangInfoDTO.getDeviceName());
            deviceDTO.setThirdDeviceId(deviceHaiKangInfoDTO.getDeviceIndexCode());
            // 设备在离线状态
            if ("online".equals(deviceHaiKangInfoDTO.getIsOnline())) {
                deviceDTO.setOnlineStatus(DeviceOnlineStatusEnum.ONLINE.getId());
            } else if ("offline".equals(deviceHaiKangInfoDTO.getIsOnline())) {
                deviceDTO.setOnlineStatus(DeviceOnlineStatusEnum.OFFLINE.getId());
            } else {
                deviceDTO.setOnlineStatus(DeviceOnlineStatusEnum.ABNORMAL.getId());
            }
            deviceDTO.setAttributeJson(new JSONObject());
            return deviceDTO;
        }).collect(Collectors.toList());
    }

}
